#!/usr/bin/python
import cgitb
cgitb.enable()
import re
import cgi 
import os

print "Content-type: text/html"
print 

class re_c:
    def __init__(self,string):
        self.string = string
    def re_cal (self):
        moudle = re.compile('(#[0-9a-fA-F]{6})')
        return re.findall(moudle,self.string)
    def re_cal_2 (self):
        moudle1 = re.compile('(name=\"\w+)')
        moudle2 = re.compile('(\w+)')
        partion_i =  re.findall(moudle1, self.string)
        bug = ""
        for i in partion_i:
            bug+=i
        result_is_bug = re.findall(moudle2, bug)
        else_is_a_bug = []
        for i2 in result_is_bug:
            if i2 != "name":
                else_is_a_bug.append(i2)
        return else_is_a_bug

class color:
    def __init__(self,path):
        self.color_file = open (path,"r")
        self.Glob = []
        self.Color_name = []
        for line in self.color_file:
            my_re = re_c(line)
            self.Glob+= my_re.re_cal()
            self.Color_name += my_re.re_cal_2()
        self.big=""
    def print_style(self):
        print """
        <style type="text/css">
        """
        for i in range(0,len(self.Glob)):
            self.big +=( "div#container"+str(i)+ " {width:100 ;height:70;background-color:"+self.Glob[i] + ";float:left;}" +"\n")
        self.big +=  """</style>"""
        return self.big
    def print_div(self):
        self.new = ""
        for i in range(0,len(self.Glob)):
            self.new += ( "<div id=\"container"+str(i)+"\" align=\"center\" >"+self.Color_name[i]+"</br>"+self.Glob[i]+ "</div>"+"\n")
        return self.new

if __name__ == "__main__":
    print "current : %s" % (os.getcwd())
    path = raw_input("Output path > ")
    output = open (path,"w")
    path_color = os.getcwd()+"/color.xml"
    my_color_html = color(path_color)
    head =  """
    <html>
    <head>\n<style type=\"text/css\">"""
    style =  my_color_html.print_style()
    link1 =  "\n</head><body>"
    div =  my_color_html.print_div()
    end_i =  "\n</body></html>"
    output.writelines(head)
    output.writelines(style)
    output.writelines(link1)
    output.writelines(div)
    output.writelines(end_i)
    output.close()
    
